window.workspace = function(){
	var response  = <?#$respons?>;
	var store, grid, win, form;
	function addForm(rows){
		if(!win) {
		    form = new Ext.form.FormPanel({
				frame		: true,
				border		: false,
				bodyStyle	: 'padding:8px;',
		        labelWidth	: 120,
		        fileUpload  : true,
		        labelAlign  : 'right',
		    	defaults	: {
		    		xtype	  : 'textfield'
		    	},
		    	items		: [{
					xtype		: 'hidden',
					name		: 'id',
					value		: 0
				},{
					xtype		: 'textfield',
					fieldLabel	: '守护名称[Name]',
					width		: 160,
					name		: 'name',
					maxLength   : 255,
					allowBlank	: false
				},{
					xtype		: 'numberfield',
					fieldLabel	: '包月单价[Price]',
					width		: 160,
					name		: 'coinprice',
					allowBlank	: false
				},{
					xtype		: 'textfield',
					fieldLabel	: '批量价格[BatchPrice]',
					width		: 240,
					name		: 'batchprice',
					emptyText   : '设置格式例如:1-500;3-1500;6-3000;',
					allowBlank	: false
				},{
					xtype		: 'combo',
					store		: new Ext.data.JsonStore({
						url	: '/admin/live/config/motor/domotor.html',
						autoDestroy: true,
						fields: ['id', 'name'],
						root: 'dataset'
					}),
					name			: 'motorid',
					hiddenName		: 'motorid',
					displayField	: 'name',
					valueField		: 'id',
					emptyText		: '请选择',
					minChars		: 1,
					 pageSize		: 10,
		            listWidth		: 258,
					width	    	: 240,
					typeAhead		: true,
					typeAheadDelay  : 0,
					triggerAction	: 'all',
					enableKeyEvents : true,
					fieldLabel		: '赠送座驾[Vehicle]',
					allowBlank		: false,
					mode			: 'remote'
				},{
					xtype		: 'textfield',
					inputType 	: 'file', 
			 		fieldLabel	: '荣耀图标[Icon]',
					name		: 'Filedata'
				},{
					xtype		: 'textarea',
					fieldLabel	: '描述[Desc]',
					width		: 320,
					name		: 'desc'
				}]
			});
			win = new Ext.Window({
				title	: '新增[Add]',
				iconCls	: 'icon-add',	
				width	: 520,
				items	: [form],
				closeAction : 'hide',
				bbar 	: {
					xtype		: 'toolbar',
					buttonAlign	: 'right',
					items		: [{
						text	: '保存[Save]',
						url		: '/admin/live/config/vipsqua/dosave.html',
						iconCls	: 'icon-save',
						handler	: function(){
						if(!form.getForm().isValid())
		        			return;
		        		form.getForm().submit({
		        			waitMsg		: GLang['30017'],
							waitTitle 	: GLang['30003'],
			        		clientValidation: true,
			        		url		: '/admin/live/config/vipsqua/dosave.html',
				        	success	: function(form, action) {
				        		store.reload();
				        		win.hide();
							},
							failure	: main.submitFailure
				    	});
					}}]
				}
			});
		}
		var id = Ext.isObject(rows)? rows.id:0;
		win.setTitle((id > 0)? '编辑[Edit]':'新增[Add]');
		win.setIconClass((id > 0)? 'icon-edit':'icon-add');
		win.show();
		form.getForm().findField('Filedata').setRawValue('');
		if(id < 1) {
			form.getForm().reset();
		} else {
			form.getForm().loadRecord(rows);
			form.getForm().findField('motorid').setRawValue(rows.get('motor'));
		}
	};
	store = new Ext.data.JsonStore({
	    autoDestroy	: true,
	    url			: '/admin/live/config/vipsqua/dolist.html',
	    remoteSort	: true,
	    sortInfo	: {
	    	field		: 'id',
	        direction	: 'DESC'
		},
		baseParams	: {
			start	: 0,
			limit	: main.pageSize
		},
		autoLoad		: true,
	    root			: 'dataset',
	 	totalProperty	: 'total',
	    fields			: ['id', 'name', 'coinprice', 'count', 'batchprice', 'motor', 'desc', 'motorid', 'iconsrc', 'stime']
	}); 
 	grid = {
		xtype		: 'grid',
		title		: response.title,
	    store		: store,
		region		: 'center', 
	    columns		: [
			{header: '编号[ID]', width:60, sortable:true, dataIndex:'id'},
			{header: '守护名称[Name]', width:120, dataIndex:'name'},
			{header: '赠送座驾[Vehicle]', width:80, dataIndex:'motor'},
			{header: '开通单价[Price]', width:80, sortable:true, dataIndex:'coinprice'},
			{header: '批量价格[BatchPrice]', width:160, sortable:true, dataIndex:'batchprice'},
			{header: '荣誉图标[Icon]', width:120, dataIndex:'iconsrc', renderer:function(v){
				if(/\.(png|jpg|jpeg|gif|bmp)$/i.test(v)) {
					 return '<img src="'+Ext.STATIC_URL+v+'"/>';
				 }
				 return '';
			}},
			{header: '描述[Desc]', width:240, dataIndex:'desc'},
			{header: '购买累计[Stats]', width:60, dataIndex:'count'},
			{header: '操作时间[LastTime]',width:120,dataIndex:'stime'}
		],
	    viewConfig	: {
	        forceFit	 : true,
	        scrollOffset : 0
	    },
	    listeners	: {
	    	'destroy':function(){
    			win && win.close();
    		},'rowdblclick':function(grid, index, e){
		    	var data = grid.store.getAt(index).copy();
		    	addForm(data);
		    }
    	},
	    sm			: new Ext.grid.RowSelectionModel({singleSelect:true}),
	    stripeRows	: true,
	    tbar		: [{
			text	: '刷新[Reload]',
			iconCls	: 'icon-refresh',
			handler : function(){
				store.reload(); 
			}
	    },'-',{
			text	: '删除[Del]',
			iconCls	: 'icon-delete',
			url		: '/admin/live/config/vipsqua/dodelete.html',
			handler : function(){
				var rcs = grid.sm.getSelections();
				if(Ext.isEmpty(rcs)) {
					Ext.Msg.alert('信息提示', '请选择要删除的记录');
					return;
				}
				var ids= [];
				Ext.each(rcs, function(item){
					ids.push(item.get('id'));
				});
				Ext.Msg.confirm('信息提示','您确定删除选中记录？删除将不能恢复',function(btn){
					if(btn=="yes") {
						Ext.Ajax.request({
							url		: '/admin/live/config/vipsqua/dodelete.html',
							params	: {'ids':ids.join(',')},
							success : function(resp, req){
								var data = Ext.decode(resp.responseText);
								if(data.code == 0) {
									store.reload(); 
								} else {
									Ext.Msg.alert('信息提示', data.msg);
								}
							}
						});
					}
				});
			}
	    },'-',{
			text	: '新增[Add]',
			url		: '/admin/live/config/vipsqua/dosave.html',
			iconCls	: 'icon-add',
			handler : function() {
				addForm(0);
			}
		}],
		bbar		: new Ext.PagingToolbar({
			pageSize	: main.pageSize,
			store		: store,
			displayInfo	: true
		})
	};
	return grid;
}();